//
//  JYJVCOfSortContent.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/3.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJVCOfSortContent.h"
#import "BaseTableView.h"
#import "BaseCollectionView.h"
#import "JYJTAMacro.h"
#import "JYJSortContentTitleCollectionViewCell.h"
#import "JYJSortModel.h"
#import "SelfSizeTool.h"
#import "JYJNetWorkParser.h"
#import "JYJSortContentTitleModel.h"
#import "JYJSortContentCollectionViewCell.h"
#import "JYJSortContentCollectionViewCellTwo.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
#import "JYJChannelModel.h"
#import "JYJMusicMiniView.h"
@interface JYJVCOfSortContent ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) BaseCollectionView *bigCollection;
@property (nonatomic, strong) BaseCollectionView *smallCollection;
@property (nonatomic, strong) NSMutableArray *marrOfTitle;
@property (nonatomic, assign) NSInteger labelIndex;
@property (nonatomic, assign) NSIndexPath *oldIndex;

@end

@implementation JYJVCOfSortContent

- (void)viewDidLoad {
    [super viewDidLoad];
    self.oldIndex = [NSIndexPath indexPathForItem:0 inSection:0];
    self.view.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self createSmallCollection];
    [self createBigCollection];
    
    if (self.sortModel) {
        self.navigationItem.title = self.sortModel.channel_name;
        [self handleData];
    }else {
        self.navigationItem.title = self.channelModel.channel_name;
    [self handelMoreData];
    }
    // Do any additional setup after loading the view.
}
- (void)handelMoreData {
    NSString *str1 = @"json=%7B%22channel_id%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%ld",self.channelModel.channel_id];
    NSString *str3 = @"%22%2C%22session%22%3A%7B%22uid%22%3A%2233366%22%2C%22sid%22%3A%2216c79d7928f744d894fe9ddf43865f0b%22%7D%7D";
    string = [[str1 stringByAppendingString:string] stringByAppendingString:str3];
   [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/channelinfo.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.marrOfTitle = [NSMutableArray array];
        NSArray *cat_listArray = result[@"data"][@"cat_list"];
        for (NSDictionary *dic in cat_listArray) {
            JYJSortContentTitleModel *model = [JYJSortContentTitleModel modelWithDic:dic];
            [self.marrOfTitle addObject:model];
        }
        [self.smallCollection reloadData];
        [self.bigCollection reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];

    
}
- (void)handleData {
    NSString *str1 = @"json=%7B%22channel_id%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%ld",self.sortModel.channel_id];
    NSString *str3 = BRACKETS_STR;
    string = [[str1 stringByAppendingString:string] stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/channelinfo.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.marrOfTitle = [NSMutableArray array];
        NSArray *cat_listArray = result[@"data"][@"cat_list"];
        for (NSDictionary *dic in cat_listArray) {
            JYJSortContentTitleModel *model = [JYJSortContentTitleModel modelWithDic:dic];
            [self.marrOfTitle addObject:model];
        }
        [self.smallCollection reloadData];
        [self.bigCollection reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (void)createSmallCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 10;
    self.smallCollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) collectionViewLayout:layout];
    [self.view addSubview:_smallCollection];
    [self.smallCollection registerClass:[JYJSortContentTitleCollectionViewCell class] forCellWithReuseIdentifier:@"Title"];
    self.smallCollection.backgroundColor = [UIColor colorWithRed:0.13 green:0.13 blue:0.15 alpha:1.00];
    self.smallCollection.delegate = self;
    self.smallCollection.dataSource = self;
    self.smallCollection.showsHorizontalScrollIndicator = NO;
}
- (void)createBigCollection  {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.bigCollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64) collectionViewLayout:layout];
    [self.view addSubview:self.bigCollection];
    self.bigCollection.pagingEnabled = YES;
    self.bigCollection.delegate = self;
    self.bigCollection.dataSource = self;
    [self.bigCollection registerClass:[JYJSortContentCollectionViewCell class] forCellWithReuseIdentifier:@"Content"];
    [self.bigCollection registerClass:[JYJSortContentCollectionViewCellTwo class] forCellWithReuseIdentifier:@"ContentTwo"];
    self.bigCollection.showsHorizontalScrollIndicator = NO;
    self.bigCollection.scrollEnabled = NO;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _smallCollection) {
        return 1 + self.marrOfTitle.count;
    }else {
        
        return 1 + self.marrOfTitle.count;
        
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _smallCollection) {
        JYJSortContentTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Title" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.titleLabel.text = @"精选";
        }else {
            JYJSortContentTitleModel *model = self.marrOfTitle[indexPath.item - 1];
            [cell setTitleModel:model];
        }
        if (indexPath.row == _labelIndex) {
            cell.titleLabel.textColor = [UIColor whiteColor];
            cell.titleLabel.font = [UIFont systemFontOfSize:20];
        }else {
            cell.titleLabel.textColor = [UIColor lightGrayColor];
            cell.titleLabel.font = [UIFont systemFontOfSize:17];
        }
        if (indexPath.row == _oldIndex.item) {
            cell.titleLabel.textColor = [UIColor whiteColor];
            cell.titleLabel.font = [UIFont systemFontOfSize:20];
        }else {
            cell.titleLabel.textColor = [UIColor lightGrayColor];
            cell.titleLabel.font = [UIFont systemFontOfSize:17];
        }

        return cell;
        
    }else {
        if (indexPath.item == 0) {
            JYJSortContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Content" forIndexPath:indexPath];
            // block
            __weak typeof(self) weakSelf = self;
            cell.handleAction = ^(JYJVCOfFeaturedTopThreeDetails *details) {
                weakSelf.tabBarController.tabBar.hidden = YES;
                weakSelf.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:details animated:YES];
                weakSelf.hidesBottomBarWhenPushed = NO;
            };
            cell.sortModel = self.sortModel;
            cell.channelModel = self.channelModel;
            return cell;
        }else {
            JYJSortContentCollectionViewCellTwo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ContentTwo" forIndexPath:indexPath];
            JYJSortContentTitleModel *model = self.marrOfTitle[indexPath.item - 1];
            __weak typeof(self) weakSelf = self;
            cell.handleAction = ^(JYJVCOfFeaturedTopThreeDetails *details) {
                weakSelf.tabBarController.tabBar.hidden = YES;
                weakSelf.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:details animated:YES];
                weakSelf.hidesBottomBarWhenPushed = NO;
            };
            // 主要取model得cat_id
            cell.titleModel = model;
            cell.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.11 alpha:1.00];
            return cell;
        }
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _smallCollection) {
        if (indexPath.item == 0) {
            return CGSizeMake(40, 40);
        }else {
            JYJSortContentTitleModel *model = self.marrOfTitle[indexPath.item - 1];
            NSString *str = model.cat_name;
            CGFloat width = [SelfSizeTool selfSizeWidthString:str height:40 font:[UIFont systemFontOfSize:20]];
            return CGSizeMake(width, 40);
        }
        
    }else {
        return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64);
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.bigCollection) {
        //当前大Collection的第几个item
        NSInteger currentItem = scrollView.contentOffset.x / SCREEN_WIDTH;
        // 根据第几个item获取当前的indexPath
        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForItem:currentItem inSection:0];
        // 根据indexpaht获取当前的标题的Collection
        JYJSortContentTitleCollectionViewCell *cell = (JYJSortContentTitleCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:currentIndexPath];
        //字体变色
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.titleLabel.font = [UIFont systemFontOfSize:20];
        // 恢复字体颜色
        NSIndexPath *indexPathPast = [NSIndexPath indexPathForItem:_labelIndex inSection:0];
        //加判防止滑一点点就就开，并没有划入新页面，字体就变色了
        if (currentIndexPath != indexPathPast) {
            JYJSortContentTitleCollectionViewCell *cell = (JYJSortContentTitleCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:indexPathPast];
            cell.titleLabel.textColor = [UIColor lightGrayColor];
            cell.titleLabel.font = [UIFont systemFontOfSize:17];
        }
        //记录当前的item 和 划过的indexPath
        self.labelIndex = currentItem;
        self.oldIndex = currentIndexPath;
    }

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.smallCollection) {
        [self.bigCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        JYJSortContentTitleCollectionViewCell *cell = (JYJSortContentTitleCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:indexPath];
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.titleLabel.font = [UIFont systemFontOfSize:20];
        JYJSortContentTitleCollectionViewCell *pastCell = (JYJSortContentTitleCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:_oldIndex];
        if (indexPath != _oldIndex) {
            pastCell.titleLabel.textColor = [UIColor lightGrayColor];
            pastCell.titleLabel.font = [UIFont systemFontOfSize:17];
        }
        self.oldIndex= indexPath;
        self.labelIndex = _oldIndex.item;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [JYJMusicMiniView shareMiniView].hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
